package com.base.data.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.data.dao.UniversityDao;
import com.base.data.entity.University;
import com.base.data.exception.DataException;
import com.mysteel.page.Page;
import com.mysteel.util.ObjectUtils;

@Service("universityService")
public class UniversityServiceImpl implements UniversityService
{
	@Autowired
	private UniversityDao universityDao;
	
	@Override
	public void saveUniversity(University university) throws DataException
	{
		if (ObjectUtils.notNull (university))
		{
			Map<String, Object> params = new HashMap<String, Object> ();
			
			params.put ("name" ,university.getName ());
			params.put ("provenceId" ,university.getProvenceId ());
			
			if (ObjectUtils.notNull (universityDao.getByParams (params)))
			{
				throw new DataException (university.getName () + "已经存在!");
			}
			
			for( int i = 1;i < 100;i++)
			{
				String id = university.getProvenceId () + "0" + i;
				
				University u = universityDao.get (id);
				
				if (ObjectUtils.isNull (u))
				{
					university.setId (id);
					university.setCreateTime (System.currentTimeMillis ());
					university.setStatus (true);
					
					universityDao.save (university);
					break;
				}
			}
			
		}
		
	}
	
	@Override
	public void deleteUniversityById(String id)
	{
		universityDao.delete (id);
	}
	
	@Override
	public void updateUniversity(University university)
	{
		
		University u = universityDao.get (university.getId ());
		
		if(ObjectUtils.notNull (u)){
			u.setName(university.getName ());
			u.setProvenceId (university.getProvenceId ());
			u.setMobile (university.getMobile ());
			u.setStatus (university.isStatus ());
			u.setDescription (university.getDescription () );
			u.setAuthorName (university.getAuthorName ());
			
			universityDao.update (u);
		}
		
	}
	
	@Override
	public University getUniversityById(String id)
	{
		
		return universityDao.get (id);
	}
	
	@Override
	public Page<University> page(Map<String, Object> params, int currentPageNum,
			int maxPageRowCount)
	{
		
		Page<University> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = universityDao.count(params);

		if (count > 0)
		{
			page = new Page<University>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<String> UniversityIds = universityDao.page(params);

			if (ObjectUtils.notEmpty(UniversityIds))
			{
				List<University> Universitys = new ArrayList<University>(UniversityIds.size());

				for (String UniversityId : UniversityIds)
				{
					Universitys.add(getUniversityById(UniversityId));
				}

				page.setDatas(Universitys);
			}
		}

		return page;
	}
	
	@Override
	public List<University> query()
	{
		List<University> us = new ArrayList<University>();
		for(String id:universityDao.query ()){
			us.add(getUniversityById (id));
		}
		
		return us;
	}
	
}
